const app = getApp()
import * as API_Members from '../../../api/members.js'
import * as API_Order from '../../../api/order.js'
import * as API_Distribution from '../../../api/distribution'
import * as API_Article from '../../../api/article'
import * as API_NetworkAuth from '../../../api/networkAuth'
import { seller_id,shareMessage } from "../../../config/config";
let WxParse = require('../../../lib/wxParse/wxParse')

Page({
  data: {
    defaultFace: '/static/images/icon-noface.jpg',
    userInfo: {},
    // 默认不显示
    showWxAuth: false,
    //待分润金额
    settlementTotal: '',
    can_rebate: '',
    //欧币
    points: '',
    orderNum: {},
    //会员级别信息
    publicMemberGrade:'',
    order_type:3,
    query: "",
    sharename:'',
    shareimg:'',
    flag:true,
    showAgreement:false
  },
  //模版消息推送测试接口
  // test(){
  //   var self = this
  //   const wx_access_token = ''
  //   // 获取access_token
  //   wx.request({
  //     url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3ffb83dc92223d96&secret=7282e3ace7b887efef911f9cabdad579',
  //     method:'GET',
  //     success: function (res) {
  //       self.test2(res.data.access_token)
  //     },
  //   })
  // },
  // test2(wx_access_token){
  //   var params = {
  //     "touser": wx.getStorageSync('testopenid'),
  //     "template_id":"HdUaTDwwNBoStWh05Mvvex_9Uxf8rlU0bphLzawDAq8",
  //     "url":"http://weixin.qq.com/download",
  //     "topcolor":"#FF0000",
  //     "data":{
  //       "thing1": {
  //         "value":"11多少啊",
  //         "color":"#173177"
  //       },
  //       "amount2":{
  //         "value":"600元",
  //         "color":"#173177"
  //       },
  //     }
  //   }
  //   wx.request({
  //     url: `https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${wx_access_token}`,
  //     data: params,
  //     method: 'POST',
  //     success: function (res) {
  //       console.log(res)
  //     },
  //     fail: function (err) {
  //       console.log(err)
  //     }
  //   })
  // },
  onLoad(options){
             // 上下级关系获取绑定
  if(options.su){
    API_Distribution.accessShortLink({ su: options.su }).then(res => {
      wx.setStorageSync('inviteCode',res.mobile)
    })
  }
  },
  async onShow() {
    API_Distribution.getsharemessage(seller_id).then( res => {
      // console.log( res) 
      this.setData({
        sharename:res.shop_mini_title,
        shareimg:res.shop_mini_share
      })
    }).catch( err => {
      console.error( err)
    })
    API_Article.getArticleByPosition('REGISTRATION_AGREEMENT').then(response => {
      // console.log(response)
      WxParse.wxParse('agreement', 'html', response.content, this)
    })
    const userInfo = wx.getStorageSync('user')
    const daifenrun = wx.getStorageSync('daifenrun')
    const can_rebate = wx.getStorageSync('can_rebate')
    const oucoin = wx.getStorageSync('oucoin')
    const shoplist = wx.getStorageSync('shoplist')    
    if (userInfo.face === 'null') {
      userInfo.face = null
    }
    this.setData({
      userInfo: userInfo,
      settlementTotal: daifenrun,
      can_rebate: can_rebate,
      points: oucoin,
      orderNum:shoplist
    })
    if (wx.getStorageSync('access_token')) {
      this.setData({
        showWxAuth: true
      })
    } else {
      this.setData({
        showWxAuth: false
      })
    }
    API_NetworkAuth.getUserInfo().then(response=>{
      this.setData({
        'userInfo.nickname':response.nickname,
        'userInfo.face':response.face
      })
    })
    API_Distribution.getAmount().then(response => {
      this.setData({
        can_rebate: response.message
      })
    });
    //获取待分润金额
    API_Distribution.getFenrun().then(response => {
      this.setData({
        settlementTotal: response.daifenrun
      })
    });
    //获取欧币
    API_Members.getPoints().then(data => {
      this.setData({
        points: data.consum_point
      })
    }); 
    /* 获取订单状态数量 */
    if(wx.getStorageSync('access_token')){
    API_Order.getOrderStatusNum().then(res => {
      this.setData({ orderNum: res });
      
    })
  }
    //获取会员级别信息
    API_Members.getPublicMemberGrade().then(res => {
      this.setData({publicMemberGrade : res})
    });
    if (wx.getStorageSync("access_token")){
    let response = await API_Distribution.generateShortLink();
    let query = response.message.substring(
      response.message.indexOf("?"),
      response.message.length
    );
    this.setData({
      query
    })
  }
  },
  login(){
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }

    // this.getUserInfo()

    // if(wx.getStorageSync('user')){
    //   getApp().autoLogin()
    //   return
    // }else{
    //   wx.navigateTo({ url: '/pages/auth/login/login' })
    // }
  },
  closeMask(){
    this.setData({flag:true})
  },
  wxLoginAuth(){
    // this.getUserInfo()
    // wx.navigateTo({
    //   url: '/pages/auth/register/register',
    // })
    this.setData({flag:true})
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.setStorageSync('wxauth', true)
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/auth/register/register',
          })
            // if (wx.getStorageSync('refresh_token')) {
            //   // wx.switchTab({
            //   //   url: '/pages/mine/mine'
            //   // })
            //   //   wx.navigateTo({
            //   //     url: '/pages/auth/register/register',
            //   // })
            // } else {
            //   app.autoLogin()
            //   wx.switchTab({
            //     url: '/pages/home/home',
            //   })
            // }
          }, 1000)
        }
      }
    })    
  },
  showagreement(){
    this.setData({showAgreement:true})
  },

    //同意协议
  handleAgreement(e) {
      const agreed = JSON.parse(e.currentTarget.dataset.agreed)
      if (!agreed) {
        wx.switchTab({ url: '/pages/home/home'})
        this.setData({showAgreement:false,flag:true})
        console.log(agreed)
      } else {
        this.setData({
          showAgreement: false,
          agreed: agreed
        })
      }
  },
  isLogin() {
    if (!wx.getStorageSync('refresh_token')) {
      wx.navigateTo({
        url: '/pages/auth/login/login'
      })
    } else {
      return true
    }
  },
  onLoad: function (options) {

  },
  clickFace() {
    if (wx.getStorageSync('refresh_token')) {
      this.goProfile()
    } else {
        this.setData({flag:false})
    }
  },
  goLogin() {
    getApp().toAutoLogin()
    setTimeout(() => {
      if (wx.getStorageSync('refresh_token')) {
        wx.showToast({
          title: '自动登录成功'
        })
      } else {
        wx.navigateTo({
          url: '/pages/auth/login/login'
        })
      }
    }, 1200)
  },
  // 我要加盟
  goJoinUs() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/my-joinus/my-joinus"
    })}
  },
  goSetting(){
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/setting/setting"
    })}

  },
  // 我的资料 
  goProfile() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/my-profile/my-profile"
    })}

  },
  goProfileUpdate() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/my-profile-update/my-profile-update"
    })}

  },
  // 开通vip
  goOpenVip() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/open-vip/open-vip"
    })}

  },
  //待分润
  goWait() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/my-wait/my-wait"
    })}

  },
  //待分润
  goAvai() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/my-avai/my-avai"
    })}

  },
  // 全部订单
  goOrder(e) {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    let type = e.currentTarget.dataset.type;
    this.setData({
      order_type:type
    })
    wx.navigateTo({
      url: "/ucenter/order/order?type="+type
    })}

  },
  payment() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/order/order?order_status=WAIT_PAY"
    })}

  },
  shipped() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/order/order?order_status=WAIT_SHIP"
    })}

  },
  received() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/order/order?order_status=WAIT_ROG"
    })}

  },
  commented() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: '/ucenter/my-comments/my-comments'
    })}
    // wx.navigateTo({
    //   url: "/ucenter/order/order?order_status=WAIT_COMMENT"
    // })

    
  },
  // 我的优惠券
  goCoupon() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/my-coupon/my-coupon"
    })}

  },
  // 我的收藏
  goCollect(e) {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: "/ucenter/my-collect/my-collect?type="+type
    })}

  },
  //我的团队
  goMyTeam() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/my-team/my-team"
    })}

  },
  //我的养老基金
  goMyFunds() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/my-funds/my-funds"
    })}

  },
  //提现
  goWithdrawal() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/my-withdrawal/my-withdrawal"
    })}

  },
  //充值
  goTopUp() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/my-topUp/my-topUp"
    })}

  },
  //收入记录,支出记录
  goIncomeRecord(e) {

    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    let type = e.currentTarget.dataset.item
    wx.navigateTo({
      url: "/ucenter/my-income/my-income?type=" + type
    })}

  },
  // 我的积分
  goPoints() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: '/ucenter/my-points/my-points'
    })}

  },
  
  goBill() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: '/ucenter/bill/bill'
    })}

  },
  //分销管理
  goDistribution() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: '/ucenter/distribution/distribution'
    })}

  },
  //账户安全
  /* goAccountSafe() {
    // if (!this.login()) return
    wx.navigateTo({
      url: "/ucenter/accountSafe/accountSafe"
    })
  }, */
  // 地址管理
  goAddress() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/address/address"
    })}

  },
  goKefu() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/kefu/kefu",
    })}

  },
  // 荣耀
  goHonour() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/honour/honour"
    })}

  },
  //分享
  goShare() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: "/ucenter/share/share"
    })}

  },
  //售后
  goAftersale() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: '/ucenter/after-sale/after-sale'
    })}

  },
 
  //资询管理
  goAsk() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: '/ucenter/my-ask/my-ask'
    })}

  },
  //我的评论
  goComments() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: '/ucenter/my-comments/my-comments'
    })}

  },
  //我的发票
  goReceipt() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: '/ucenter/my-receipt/my-receipt'
    })}

  },
  //增票资质
  goIncreaseTicket() {
    // if (!this.login()) return
    if(!wx.getStorageSync('user')){
      this.setData({flag:false})
    }
    else{    wx.navigateTo({
      url: '/ucenter/increase-ticket/increase-ticket'
    })}

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true,
    });
    return {
      title: this.data.sharename,
      desc: this.data.sharename,
      path: '/pages/home/home' + this.data.query,
      imageUrl: this.data.shareimg
    }
  },
  // 一键拨号
  callUs(){
    wx.showActionSheet({
      itemList:['400-005-1665'],
      success:(res) => {
        wx.makePhoneCall({
          phoneNumber: '400-005-1665',
          success:() => {
            console.log('success')
          },
          fail:() => {
            console.log('error')
          }
        })
      }
    })
  },
  

  
})